package com.hcbd.tdwt.ui.xingyu.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.ui.xingyu.vo.SatelliteVO;

import java.util.List;

public class SatelliteListAdapter extends RecyclerView.Adapter<SatelliteListAdapter.MyViewHolder> {

    protected final Context mContext;
    private List<SatelliteVO> satelliteVOS;
    private final LayoutInflater mInflater;
    private Drawable china,usa,eu,russia,other;

    private int viewId;
    public SatelliteListAdapter(Context context, List<SatelliteVO> list) {
        mContext = context;
        mInflater = ((Activity) context).getLayoutInflater();
        satelliteVOS = list;
        china=context.getDrawable(R.drawable.china);
        usa=context.getDrawable(R.drawable.usa);
        eu=context.getDrawable(R.drawable.eu);
        russia=context.getDrawable(R.drawable.russia);
        other=context.getDrawable(R.drawable.mix);
        if(BdToolFactory.app_type==BdToolFactory.XY_BG_TYPE||BdToolFactory.app_type==BdToolFactory.XY_PDA_BG_TYPE){
            viewId=R.layout.satelite_item;
        }else{
            viewId=R.layout.satelite_item_other;
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view =mInflater.inflate(viewId, viewGroup, false);
        return new MyViewHolder(view);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        SatelliteVO satelliteVO = satelliteVOS.get(i);
        String type=satelliteVO.getSatelliteType();//BD 北斗 GP 美国 GL俄罗斯 GA 欧盟 GN混合
        if(type.contains("BD")||type.contains("PQ")||type.contains("GB")){
            myViewHolder.satelliteTypeImg.setBackground(china);
        }else if(type.contains("GP")){
            myViewHolder.satelliteTypeImg.setBackground(usa);
        }else if(type.contains("GL")){
            myViewHolder.satelliteTypeImg.setBackground(russia);
        }else if(type.contains("GA")){
            myViewHolder.satelliteTypeImg.setBackground(eu);
        }else{
            myViewHolder.satelliteTypeImg.setBackground(other);
        }
        String satelliteNumStr = satelliteVO.getSv();
        if(type.contains("BD")||type.contains("PQ")||type.contains("GB")){
            satelliteNumStr = "2"+satelliteNumStr;
        }else if(type.contains("GA")){
            satelliteNumStr = "3"+satelliteNumStr;
        }
        myViewHolder.satelliteNum.setText(satelliteNumStr);
        String cno=satelliteVO.getCno();
        if(!"".equals(cno)){
            myViewHolder.signalProgress.setProgress(Double.valueOf(satelliteVO.getCno()).intValue());
            myViewHolder.signalTV.setText(cno);
        }else{
            myViewHolder.signalProgress.setProgress(0);
            myViewHolder.signalTV.setText("0");
        }

        myViewHolder.azimuthTV.setText(satelliteVO.getAz());
        myViewHolder.elevationAngleTV.setText(satelliteVO.getElv());
    }

    @Override
    public int getItemCount() {
        return satelliteVOS.size();
    }

    public void notifyDataSetChanged(List<SatelliteVO> list) {
        satelliteVOS = list;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView satelliteTypeImg;
        TextView satelliteNum;
        ProgressBar signalProgress;
        TextView signalTV;
        TextView azimuthTV;
        TextView elevationAngleTV;
        LinearLayout satelliteNumLL;
        RelativeLayout signalRL;
        MyViewHolder(View itemView) {
            super(itemView);
            satelliteTypeImg=itemView.findViewById(R.id.satelliteTypeImg);
            satelliteNum=itemView.findViewById(R.id.satelliteNum);
            signalProgress=itemView.findViewById(R.id.signal_progress);
            signalTV=itemView.findViewById(R.id.signalTV);
            azimuthTV=itemView.findViewById(R.id.azimuthTV);
            elevationAngleTV=itemView.findViewById(R.id.elevationAngleTV);
            satelliteNumLL=itemView.findViewById(R.id.satelliteNumLL);
            signalRL=itemView.findViewById(R.id.signalRL);
        }
    }



    @Override
    public long getItemId(int position) {
        return position;
    }


}